package com.isoft.c2team3service2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.isoft.c2team3service2.entity.PerformanceInformation;
import com.isoft.c2team3service2.mapper.PerformanceInformationMapper;
import com.isoft.c2team3service2.service.PerformanceInformationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.isoft.c2team3service2.util.RedisConstants.ARTIST_SHOW;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 蓝莓
 * @since 2025-09-11
 */
@Service
public class PerformanceInformationServiceImpl extends ServiceImpl<PerformanceInformationMapper, PerformanceInformation> implements PerformanceInformationService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private PerformanceInformationMapper performanceInformationMapper;

    @Autowired
    private ObjectMapper objectMapper;


//    # 选择淘汰策略（二选一）：
//   1. allkeys-lru：所有键中，淘汰最近最少使用（LRU）的键
//   2. volatile-lru：仅淘汰设置了过期时间且最近最少使用的键
//    maxmemory-policy allkeys-lru

    @Override
    public List<PerformanceInformation> getInformation() {
        List<PerformanceInformation> list = performanceInformationMapper.selectList(new QueryWrapper<>());

        for (PerformanceInformation info : list) {
            try {
                // 生成 Redis 键：ARTIST_SHOW:{artist}（如 ARTIST_SHOW:华语群星）
                String redisKey = ARTIST_SHOW + ":" + info.getArtist();
                // 将 PerformanceInformation 对象转为 JSON 字符串
                String jsonValue = objectMapper.writeValueAsString(info);
                // 存入 Redis
                stringRedisTemplate.opsForValue().set(redisKey, jsonValue);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return list;
    }
}